const { resolve } = require('path'),
  HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  // 模式 development or production
  mode: 'development',

  devtool: 'source-map',

  // 入口
  entry: {
    index: resolve(__dirname, 'src/js/index.js'),
    // demo: resolve(__dirname, 'src/js/demo.js')
  },

  // 出口
  output: {
    path: resolve(__dirname, 'dist'),
    filename: 'js/bundle.js'
  },

  // 模块：文件转换
  module: {
    // 规则
    rules: [
      {
        test: /\.js$/,
        exclude: __dirname + 'node_modules',
        include: __dirname + 'src',
        loader: 'babel-loader',
      },

      {
        test: /\.(css|less)$/,
        use: [
          'style-loader',
          'css-loader',
          'less-loader'
        ]
      },

      // {
      //   test: require.resolve('jquery'), //require.resolve 用来获取模块的绝对路径
      //   use: [{
      //     loader: 'expose-loader',
      //     options: 'jQuery'
      //   }, {
      //     loader: 'expose-loader',
      //     options: '$'
      //   }]
      // }
    ]
  },

  resolve: {},

  // externals: {
  //   jquery: 'window.$'
  // },

  // 插件
  plugins: [
    new HtmlWebpackPlugin({
      minify: {
        // 去除空白字符（空行）和注释
        removeComments: true,
        collapseWhitespace: true
      },
      filename: 'index.html',
      template: resolve(__dirname, 'src/index.html'),
      excludeChunks: ['node_modules'],
      files: {
        js: ['js/index.js'],
        chunks: {
          'main': {
            'entry': 'dist/bundle.js'
          }
        }
      }
    }),
    new HtmlWebpackPlugin({
      minify: {
        // 去除空白字符（空行）和注释
        removeComments: true,
        collapseWhitespace: true
      },
      filename: 'demo.html',
      template: resolve(__dirname, 'src/demo.html'),
      excludeChunks: ['node_modules'],
      files: {
        js: ['js/demo.js'],
        chunks: {
          'main': {
            'entry': 'dist/js/bundle.js'
          }
        }
      }
    }),
  ],

  // 本地服务器
  devServer: {
    // 监听选项
    watchOptions: {
      ignored: /node_modules/
    },
    open: true,
    host: 'localhost',
    port: 3001
  }
}